package com.tz.cartelo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tz.cartelo.R;
import com.tz.cartelo.adapter.MessageAdapter;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.InformationDetails;
import com.tz.cartelo.result.Result;
import com.tz.cartelo.result.ResultMessage;
import com.tz.cartelo.util.CommonUtils;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2017/4/16.
 */

public class SystemMessageActivity extends DataLoadActivity implements View.OnClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    private static final String TAG = "SystemMessageActivity";
    private static final int MESSAGE_DETAILS = 1;
    private Api mApi;
    private int mPageNo;
    private BGARefreshLayout mRefreshLayout;
    private boolean mCanLoadMore = true;
    private List<InformationDetails> mInformationDetailsList;
    private MessageAdapter mMessageAdapter;
    private int mPosition;
    private boolean isFirstLoad = true;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_system_set;
    }

    @Override
    protected void init() {
        mRefreshLayout = (BGARefreshLayout) findViewById(R.id.fresh_layout);
        mRefreshLayout.setDelegate(this);
        CommonUtils.setBGARefreshLayout(mRefreshLayout, this);

        mInformationDetailsList = new ArrayList<>();
        ListView message_list = (ListView) findViewById(R.id.message_list);
        mMessageAdapter = new MessageAdapter(this, mInformationDetailsList);
        message_list.setAdapter(mMessageAdapter);
        message_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPosition = position;
                Bundle bundle = new Bundle();
                bundle.putSerializable("message", (Serializable) mInformationDetailsList.get(position));
                switchActivityForResult(MessageDetailsActivity.class, MESSAGE_DETAILS, bundle);
            }
        });

        mPageNo = 1;
        mApi = Api.MESSAGE_INFO;
        loadData(mApi, true);
    }

    @Override
    protected void initTitle() {
        ImageView titleBack = (ImageView) findViewById(R.id.title_back);
        titleBack.setVisibility(View.VISIBLE);
        titleBack.setOnClickListener(this);

        TextView titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText(R.string.manager_center_system_message);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finishActivityForResult();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case MESSAGE_INFO:
                params.addParam("pageNo", mPageNo + "");
                params.addParam("userId", PreferencesUtils.getString(Key.PER_USER_ID, ""));
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case MESSAGE_INFO:
                mRefreshLayout.endRefreshing();
                mRefreshLayout.endLoadingMore();

                ResultMessage resultMessage = fromJson(response, ResultMessage.class);
                if (!resultMessage.isSuccess()) {
                    showToast(resultMessage.getMessage());
                    return;
                }
                mInformationDetailsList.addAll(resultMessage.getInformation().getInformationDetailsList());
                mMessageAdapter.notifyDataSetChanged();

                if (mPageNo > 1) {
                    if (resultMessage.getInformation().getInformationDetailsList().isEmpty()) {
                        mCanLoadMore = false;
                        mPageNo--;
                    }
                }

                if (isFirstLoad) {
                    mPageNo++;
                    loadData(Api.MESSAGE_INFO, true);
                    isFirstLoad = false;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case MESSAGE_DETAILS:
                    if (mInformationDetailsList.get(mPosition).getIsRead().equals("0")) {
                        mInformationDetailsList.get(mPosition).setIsRead("1");
                        mMessageAdapter.notifyDataSetChanged();
                    }
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        isFirstLoad = true;
        mPageNo = 1;
        mCanLoadMore = true;
        mInformationDetailsList.clear();
        loadData(Api.MESSAGE_INFO, true);
        mRefreshLayout.endRefreshing();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (!mCanLoadMore) {
            mRefreshLayout.endLoadingMore();
            return false;
        } else {
            mPageNo++;
            loadData(Api.MESSAGE_INFO, true);
            return true;
        }
//        mRefreshLayout.endLoadingMore();
//        return false;
    }

    @Override
    public void onBackPressed() {
        finishActivityForResult();
        super.onBackPressed();
    }
}
